package com.controller;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.StaticMapUtil;
import com.Util;
import com.client.HttpClientService;

/** 
* @author  Jrxsr
* @version 2018年11月10日
* 企业中心
*/
@Controller
@Scope("prototype")
public class PController {
	
	static Logger log = Logger.getLogger(PController.class);
//	Jedis jedis;
	public PController() {
		log.error("PController--构造了");
		//this.sessionId="01";
	}
	@Autowired
	HttpClientService httpClientServiceImpl;
	private static String serverIP;

	public static void setServerIP(String serverIP) {
		// 修改IP可以通过修改redis数据库serverIP的值修改
		PController.serverIP = serverIP;
		log.error(serverIP);
	}

	// 连接服务器的方法
	public String httppost(String method, Map map, HttpServletRequest request) throws Exception {
		try {
			String str = httpClientServiceImpl.doPost(serverIP+method,map);
			log.error("方法："+method+"---返回结果："+str);
			return str;
		}catch (Exception e) {
			log.error("服务器连接超时。异常信息："+e.toString());
			return "服务器连接超时。异常信息："+e.toString();
		}
	}

	
	// 放入sessionid
	@RequestMapping(value="/setsessionidcommeg",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public void setsessionidcommeg(@RequestParam Map<String,String> map , HttpServletRequest request) throws Exception {
		//map.put("cuid", "9527");
		if(map.get("sessionId")!=null) {
			StaticMapUtil.getmap().put("sessioncommeg", map.get("sessionId"));
			System.err.println("获取到接口发送的用户!");
		}else{
			System.err.println("没有获取到接口发送的值?");
		};
	}
	
	// 获取企业信息
	@RequestMapping(value="/getcominfo",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String getcominfo(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		//map.put("comid", request.getSession().getAttribute("id"));
		if(StaticMapUtil.getmap().get("sessioncommeg")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("sessioncommeg"));
		}else {
			map.put("comid", "01");
		}
		return this.httppost("cominfo", map, request);
	}
	
	

	
	// 获取企业员工
	@RequestMapping(value="/getcommeg",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String getusermsg(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		if(StaticMapUtil.getmap().get("sessioncommeg")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("sessioncommeg"));
		}else {
			map.put("comid", "01");
		}
		return this.httppost("querycom", map, request);
	}
	
	// 获取企业员工个人订单
	@RequestMapping(value="/getuserinfo",method=RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String getuserinfo(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		return this.httppost("order", map, request);
	}
	
	// 获取企业员工个人价值分体系
	@RequestMapping(value="/getusergrant",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String getusergrant(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		if(StaticMapUtil.getmap().get("sessioncommeg")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("sessioncommeg"));
		}else {
			map.put("comid", "01");
		}
		return this.httppost("getinfogrant", map, request);
	}
	
	// 导出企业员工
	@RequestMapping(value="/outexaxluserinfo",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String outexaxluserinfo(@RequestParam Map<String,Object> map, HttpServletRequest request ,HttpServletResponse response) throws Exception {
		//map.put("comid", request.getSession().getAttribute("id"));
		if(StaticMapUtil.getmap().get("sessioncommeg")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("sessioncommeg"));
		}else {
			map.put("comid", "01");
		}
		OutputStream outputStream = null;
		try {
			String str = this.httppost("querycom", map, request);
			Map<String, Object> comuser = Util.JSONToMap(str);
			List<Map> list = (List<Map>) comuser.get("data");
			
//			//excel标题
//		    String[] title = {"ID","员工名","员工头像地址","员工身份证号","注册时间","生日","地址","认证图","状态"};
	//	
//		    //excel文件名
		    String fileName = "员工档案表"+System.currentTimeMillis()+".xls";
	//	
//		    //sheet名
//		    String sheetName = "员工档案表";
			
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFCellStyle cellStyle = workbook.createCellStyle(); 
			HSSFSheet sheet = workbook.createSheet("测试文件");
			HSSFRow row1 = sheet.createRow(0);
			HSSFCell cell = row1.createCell(0);
			cell.setCellValue("员工档案表"); 
			cellStyle.setAlignment(HorizontalAlignment.CENTER);
			// 合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8));
			// 在sheet里创建第二行
			HSSFRow row2 = sheet.createRow(1);
			// 创建单元格并设置单元格内容
			row2.createCell(0).setCellValue("ID");
			row2.createCell(1).setCellValue("员工名");
			row2.createCell(2).setCellValue("员工头像地址");
			row2.createCell(3).setCellValue("员工身份证号");
			row2.createCell(4).setCellValue("注册时间");
			row2.createCell(5).setCellValue("生日");
			row2.createCell(6).setCellValue("地址");
			row2.createCell(7).setCellValue("认证图片地址");
			row2.createCell(8).setCellValue("状态");
			
			int i=2;
			for (Map ll : list) {
				int j=0;
				// 在sheet里创建内容行
				HSSFRow row = sheet.createRow(i);
				row.createCell(j).setCellValue((String) ll.get("inid"));
				j++;
				row.createCell(j).setCellValue((String) ll.get("inname"));
				j++;
				row.createCell(j).setCellValue((String) ll.get("inimg"));
				j++;
				row.createCell(j).setCellValue((String) ll.get("inidc"));
				j++;
				row.createCell(j).setCellValue((String) ll.get("rtime"));
				j++;
				row.createCell(j).setCellValue((String) ll.get("inbirth"));
				j++;
				row.createCell(j).setCellValue((String) ll.get("inadd"));
				j++;
				row.createCell(j).setCellValue((String) ll.get("identification"));
				j++;
				row.createCell(j).setCellValue((String) ll.get("state"));
				i++;
			}
			fileName = new String(fileName.getBytes(),"ISO8859-1");
			response.setContentType("application/octet-stream;charset=ISO8859-1");
	        response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
	        response.addHeader("Pargam", "no-cache");
	        response.addHeader("Cache-Control", "no-cache");
	        outputStream = response.getOutputStream();
			workbook.write(outputStream);
			return "success";
		}catch (Exception e) {
			return "error";
		}finally {
			if(outputStream!=null) {
				outputStream.close();
			}
		}
		
	}
	

	
}
